package my.crawler.impl.navigation;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.w3c.dom.Document;

import my.crawler.data.ObjectData;

public class MultyObjectPage extends Page {
	
	public List<ObjectData> getObjects(URL url) {
		List<ObjectData> objects = null;
		//TODO: load doc
		Document doc = null;
		//TODO: count mismatch
		int count = objectMapping.fieldMappings.get(0).getFieldValues(null).size();
		
		for(FieldMapping mapping : objectMapping.fieldMappings) {
			List<String> values = mapping.getFieldValues(doc);
			if(objects == null) {
				objects = buildList(values.size());
			}
			Assert.assertEquals("Xpath return different count of fields",objects.size(), values.size());
			for(int i = 0; i < values.size();i++) {
				objects.get(0).put(mapping.name, values.get(0));
			}
		}
		return objects;
	}
	
	private List<ObjectData> buildList(int count) {
		List<ObjectData>  objects = new ArrayList<ObjectData>(count);
		for(int i=0;i<count;++i) {
			objects.add(new ObjectData());
		}
		return objects;
	}
}
